<template>
  <div class="login_container">
    <!-- 头像 -->
    <div class="login_box">
      <div class="avatar_box">
        <img src="../../assets/logo.png">
      </div>
      <!--表单区域 -->
      <el-form class="login_form" :model="LoginForm" :rules="LoginFormRule" ref="LoginFormRef" label-width="0px">
        <el-form-item prop="mobile">
          <el-input v-model="LoginForm.mobile" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="LoginForm.password" type="password" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" :loading="loading" @click="onSubmit">登录</el-button>
          <el-button type="info" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <!--- 验证码区域 -->
      <Vcode :show="isShow"
             @success="success" @close="close"/>
    </div>
  </div>
</template>

<script>
import Vcode from 'vue-puzzle-vcode'

export default {
  components: {Vcode},
  data() {
    return {
      isShow: false, //验证码是否显示
      imgCode: undefined,
      loading:false,
      //登录表单绑定
      LoginForm: {
        mobile: '18888888888',
        password: '111111'
      },
      userInfo:[],//存储用户信息
      //验证规则
      LoginFormRule: {
        mobile: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 11, message: '长度在 3 到 11 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入登录密码', trigger: 'blur'},
          {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.LoginFormRef.validate(async (valid) => {
        if (!valid) return;
        this.isShow = true;
      })
    },
    onReset() {
      this.$refs.LoginFormRef.resetFields()
    },
    //验证器通过时
    async success() {
      const {data: res} = await this.$http.post('login', this.LoginForm)
      if (res.code !== 200) return this.$message.error("登录失败")
      this.$message.success("登录成功")
      window.sessionStorage.setItem("token", res.data.content.api_token)
      this.getUserInfo();
      this.isShow = false; // 通过验证后，需要手动隐藏模态框
      this.loading = false;
    },
    //获取用户信息
    async getUserInfo() {
      const {data:res} = await this.$http.get('dashboard/user_show')
      if(res.code !== 200) return this.$message.error(res.data.message)
      this.userInfo = res.data.list;
      this.$store.commit("setUserInfo",res.data.list)
      //跳转路由
      this.$router.push('/home')
    },
    //关闭验证器
    close() {
      this.isShow = false;
    }

  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #f7f7f7;
  background-image: url("../../assets/image/body.svg");
  height: 100%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12);
  background-size: 100%;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #ffffff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      height: 130px;
      width: 130px;
      border: 1px solid #eeeeee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #dddddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #ffffff;
      img {
        height: 130px;
        width: 130px;
        border-radius: 50%;
        background-color: #eeeeee;
      }

    }
    .btns {
      display: flex;
      justify-content: flex-end;
    }
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
    }
  }
}



</style>
